package org.lex.ftplist.ui.common;

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingWorker;

import org.apache.commons.net.ftp.FTPClient;
import org.lex.collection.tree.ITreeNode;
import org.lex.ftplist.core.FtpList;
import org.lex.ftplist.core.FtpUrl;
import org.lex.ftplist.ui.FLApplication;
import org.lex.ftplist.ui.FtpTreeFrame;
import org.lex.ftplist.util.FtpUtils;

public class FtpListPane extends JPanel {
	private JLabel ftpLabel;
	private JTextField ftpTextField;
	private JScrollPane logScrollPane;
	private JTextArea logTextArea;
	private JLabel pathLabel;
	private JTextField pathTextField;
	private JProgressBar progressBar;

	public FtpListPane() {
		initComponents();
	}

	public void runFtpList(final FtpUrl url) {
		this.ftpTextField.setText(url.toString());
		final FtpList list = new FtpList();
		progressBar.setModel(list);
		new SwingWorker<ITreeNode, Void>() {
			@Override
			protected ITreeNode doInBackground() throws Exception {
				try {
					FTPClient ftp = FtpUtils.connect(url);
					return list.list(ftp, url.path);
				} catch (Exception e) {
					e.printStackTrace();
					throw e;
				}
			}

			@Override
			protected void done() {
				try {
					for (Container p = FtpListPane.this; p != null; p = p.getParent()) {
						if (p instanceof JInternalFrame) {
							((JInternalFrame) p).dispose();
						}
					}
					ITreeNode root = this.get();
					FtpTreeFrame frame = new FtpTreeFrame(url, root);
					FLApplication.getInstance().getOperator().addInternalFrame(frame);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.execute();
	}

	private void initComponents() {
		GridBagConstraints gridBagConstraints;

		ftpLabel = new JLabel();
		ftpTextField = new JTextField();
		pathLabel = new JLabel();
		pathTextField = new JTextField();
		progressBar = new JProgressBar();
		logScrollPane = new JScrollPane();
		logTextArea = new JTextArea();

		setLayout(new GridBagLayout());

		ftpLabel.setText("FTP");
		ftpLabel.setName("ftpLabel"); // NOI18N
		gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.anchor = GridBagConstraints.WEST;
		gridBagConstraints.insets = new Insets(12, 12, 5, 5);
		add(ftpLabel, gridBagConstraints);

		ftpTextField.setName("ftpTextField"); // NOI18N
		ftpTextField.setEditable(false);
		gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.insets = new Insets(12, 0, 5, 11);
		add(ftpTextField, gridBagConstraints);

		pathLabel.setText("Path");
		pathLabel.setName("pathLabel"); // NOI18N
		gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.anchor = GridBagConstraints.WEST;
		gridBagConstraints.insets = new Insets(0, 12, 5, 5);
		add(pathLabel, gridBagConstraints);

		pathTextField.setName("pathTextField"); // NOI18N
		pathTextField.setEditable(false);
		gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.insets = new Insets(0, 0, 5, 11);
		add(pathTextField, gridBagConstraints);

		progressBar.setName("progressBar"); // NOI18N
		gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 2;
		gridBagConstraints.gridwidth = 2;
		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.insets = new Insets(0, 12, 5, 11);
		add(progressBar, gridBagConstraints);

		logScrollPane.setName("logScrollPane"); // NOI18N

		logTextArea.setName("logTextArea"); // NOI18N
		logScrollPane.setViewportView(logTextArea);

		gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 3;
		gridBagConstraints.gridwidth = 2;
		gridBagConstraints.fill = GridBagConstraints.BOTH;
		gridBagConstraints.weightx = 10.0;
		gridBagConstraints.weighty = 10.0;
		gridBagConstraints.insets = new Insets(0, 12, 11, 11);
		add(logScrollPane, gridBagConstraints);
	}
}
